import requests
import json

key ="457a539845c2f61d231796bf5b0779b8"  #高德的key 


def get_geo(address):  #获取经纬度api
    url = "https://restapi.amap.com/v3/geocode/geo"
    params = {'key': '{}'.format(key),'address': '{}'.format(address)}
    try: #增加异常判断
        res = requests.get(url,params)
        jd = json.loads(res.text) #json格式化高德的返回
        coords = jd['geocodes'][0]['location']  #获取经纬度
    except Exception as e:
        coords = 'null'
    return  coords


def cheack_walk(origin,destination): #获取步行的返回
    url = "https://restapi.amap.com/v3/direction/walking"
    params = {'key': '{}'.format(key), 'origin': '{}'.format(origin),'destination':'{}'.format(destination)}
    try:
        res = requests.get(url, params)
        jd = json.loads(res.text)  # json格式化高德的返回
        a=jd["route"]["paths"][0]['steps']
        x = []
        for k in range(0, len(a)):
            x.append([a[k].get('instruction')])  # 各步骤添加到列表中

    except Exception as e:
        return 'null'
    return x

def bicycling(origin,destination): #获取步行的返回
    url = "https://restapi.amap.com/v4/direction/bicycling"
    params = {'key': '{}'.format(key), 'origin': '{}'.format(origin),'destination':'{}'.format(destination)}
    try:
        res = requests.get(url, params)
        jd = json.loads(res.text)  # json格式化高德的返回
        a=jd["data"]["paths"][0]['steps']
        x = []
        for k in range(0, len(a)):
            x.append([a[k].get('instruction')])  # 各步骤添加到列表中

    except Exception as e:
        return 'null'

    return x

def driving(origin,destination): #获取的返回
    url = "https://restapi.amap.com/v3/direction/driving"
    params = {'key': '{}'.format(key), 'origin': '{}'.format(origin),'destination':'{}'.format(destination)}
    try:
        res = requests.get(url, params)
        jd = json.loads(res.text)  # json格式化高德的返回
        a=jd["route"]["paths"][0]['steps']
        x = []
        for k in range(0, len(a)):
            x.append([a[k].get('instruction')])  # 各步骤添加到列表中
    except Exception as e:
        return  'null'

    return  x


def weather(city): #获取公交的返回
    url = "https://restapi.amap.com/v3/config/district"
    params = {'key': '{}'.format(key), "keywords":'{}'.format(city)}
    try:
        res = requests.get(url, params)
        jd = json.loads(res.text)  # json格式化高德的返回
        adcode = jd['districts'][0]['adcode']  #获取城市编码
        url = "https://restapi.amap.com/v3/weather/weatherInfo"
        params = {'key': '{}'.format(key), "city": '{}'.format(adcode)}
        res = requests.get(url, params)          #根据城市编码获取 城市天气情况
        jd = json.loads(res.text)  # json格式化高德的返回
        reporttime = str(jd['lives'][0]['reporttime'])  #预报时间
        city = str(jd['lives'][0]['city'])  #城市
        weather = str(jd['lives'][0]['weather']) #天气
        temperature = str(jd['lives'][0]['temperature']) #温度
        windpower = str(jd['lives'][0]['windpower'])  #风力

        result="预报时间："+reporttime+"当前"+city+"天气"+weather+"，气温为"+temperature+"，风力"+windpower+"级。" #组装语句
        #print(result)
    except Exception as e:
        result = 'null'
    #print(result)
    return result



